<?php

class news
{
	var $mode;
	var $id;
	var $news_article;
	var $items = Array();
	var $news_owner = Array();
	var $ok = false;
	
	function news($id, $data, $mode)
	{
		global $db, $lang; 
		
		if($id)
			$this->id = $data;
		else
			$this->id = $data['n_id'];
		
		$this->saveAdminAction();
		
		if($mode >= NEWS_MODE_FULL && $mode <= NEWS_MODE_TITLE)
		{
			if(! $id)
				$this->news_article = $data;
				
			if(!$id || $this->getDataFromID($data)) 
			{			
				$article = $this->news_article;		
				
				if($mode == NEWS_MODE_FULL)
				{
				
					$q = str_replace('{ID}', $article['n_owner'] ,$article['c_selectidquery']);
					$result = $db->sql_query($q);
					if($item = $db->sql_fetchrow($result)) 
					{
						$this->news_owner['id'] = $article['n_owner']; 
						$this->news_owner['link'] = str_replace('{ID}', $article['n_owner'], $article['c_viewurl']);
						$this->news_owner['name'] = $item[$article['c_selectname']];
	
						$this->news_owner['image'] = decodeAvatar($item[$article['c_selectimage']],$item[$article['c_selectimagetype']]);
						$this->news_owner['thmb'] = decodeAvatar($item[$article['c_selectthmb']],$item[$article['c_selectimagetype']]);
							
						include_once 'includes/comments.php';
				
						if($article['n_commentshide'] == 0)
						{
							if($article['n_commentslocked'] == 0)
							{
								//3 = news, $id = which incident comments to show, 0 = show all comments
								$this->items[] = new listComments(3, $article['n_id'], $lang['CommentTitle'], 0); 
								
								$this->items[] = new addComment(3, $article['n_id'], 'news.php?mode=0&id=' . $article['n_id'], str_replace('{ID}', $article['n_title'], $lang['CommentGenericTitle']), 
																	'', ''); 
							}
							else
							{
								//3 = news, $id = which incident comments to show, 0 = show all comments, hide comments if none.
								$this->items[] = new listComments(3, $article['n_id'], $lang['CommentTitle'] . ' (' . $lang['CommentLocked'] . ')', 0, true); 
								
							}
						}
						
						$this->items[] = new newsAdminFunctions('news.php?mode=0&id=' . $article['n_id'], $article['n_showsticky'], 
								$article['n_commentshide'] == 1, $article['n_commentslocked'] == 1, $article['n_galleryhide'] == 1, $this->id);
	
						$this->mode = $mode;
						$this->ok = true;
					}
				}
				else if($mode == NEWS_MODE_MINI || $mode == NEWS_MODE_TITLE)
				{
					$this->mode = $mode;
					$this->ok = true;
				}
			}
		}
	}
	
	function ok()
	{
		return $this->ok;
	}
	
	function render()
	{
		if($this->ok)
		{
			if($this->mode == NEWS_MODE_FULL)
			{
				$this->renderFullArticle();
			}
			else if($this->mode == NEWS_MODE_MINI)
			{
				$this->renderMiniArticle();
			}
			else if($this->mode == NEWS_MODE_TITLE)
			{
				$this->renderTitle();
			}
		}
	}
	
	function renderFullArticle()
	{
		global $lang, $skin_dir;
		$this->news_article = formatArrayOutput($this->news_article);
		$author = '<a href="' . $this->news_owner['link'] .	'">' . $this->news_owner['name'] . '</a>';
		$catagory = '<a href="news.php?catagory=' . $this->news_article['n_catagory'] . '">' . $this->news_article['c_title'] . '</a>';
					
		
		$data = $this->news_article;
		
		$data['n_title'] = '<a href="news.php">' . $this->news_article['n_title'] . '</a>';
	
		if($this->news_owner['image'] != '')
			$data['n_logo'] = '<a href="' . $this->news_owner['link'] .	'"><img class="tableArticleImage" src="' . $this->news_owner['image'] . '"/></a>';
		else
			$data['n_logo'] = '';
	
		$data['n_author'] = sprintf($lang['NewsPostedBy'], $author , $this->news_article['n_posteddateF'], $catagory) ;
		
		if($data['n_showsticky'])
			$data['n_sticky'] = sprintf($lang['NewsStickied'], $this->news_article['n_stickyexpiryF']);
		else
			$data['n_sticky'] = '';
			
		
		include $skin_dir . 'tpl/newsFullArticle.tpl';
		
		foreach($this->items as $item)
		{
			$item->render();
		}
	}
	
	function renderMiniArticle()
	{
		global $lang, $skin_dir;
		$this->news_article = formatArrayOutput($this->news_article);
		
		$author = '<a href="' . str_replace('{ID}', $this->news_article['n_owner'], $this->news_article['c_viewurl']) .	'">' . $this->news_article['n_ownername'] . '</a>';
		$catagory = '<a href="news.php?catagory=' . $this->news_article['n_catagory'] . '">' . $this->news_article['c_title'] . '</a>';
		
		$data = $this->news_article;
	
		if($this->news_article['comment_count'] == 1)
			$comments = sprintf($lang['NewsComment'], $this->news_article['comment_count']);
		else
			$comments = sprintf($lang['NewsComments'], $this->news_article['comment_count']);
			
			
		$data['n_author'] = sprintf($lang['NewsPostedByComments'], $author , $this->news_article['n_posteddateF'], $catagory, $comments) ;
		
		if($data['n_showsticky'])
			$data['n_sticky'] = '<img style="float: left" src="' . $skin_dir . 'images/icn_sticky.png"/>';
		else
			$data['n_sticky'] = '';
		
		$data['n_title'] = '<a href="news.php?id=' . $this->news_article['n_id'] . '&mode=0">' . $data['n_title'] . '</a>';
		$data['n_readmore'] = '<a href="news.php?id=' . $this->news_article['n_id'] . '&mode=0">' . $lang['NewsReadMore'] . '</a>';
		
		include $skin_dir . 'tpl/newsMiniArticle.tpl';
		
	}
	
	function renderTitle()
	{
		global $lang, $skin_dir;
		$this->news_article = formatArrayOutput($this->news_article);
		
		$author = '<a href="' . str_replace('{ID}', $this->news_article['n_owner'], $this->news_article['c_viewurl']) .	'">' . $this->news_article['n_ownername'] . '</a>';
		$catagory = '<a href="news.php?catagory=' . $this->news_article['n_catagory'] . '">' . $this->news_article['c_title'] . '</a>';
		
		$data = $this->news_article;
	
			
		$data['n_author'] = sprintf($lang['NewsPostedByShort'], $author , $this->news_article['n_posteddateF'], $catagory) ;
		
		if($data['n_showsticky'])
			$data['n_sticky'] = '<img style="float: left" src="' . $skin_dir . 'images/icn_sticky.png"/>';
		else
			$data['n_sticky'] = '';
		
		$data['n_title'] = '<a href="news.php?id=' . $this->news_article['n_id'] . '&mode=0">' . $data['n_title'] . '</a>';
		
		include $skin_dir . 'tpl/newsTitleArticle.tpl';
		
	}
	
	function getDataFromID($id)
	{
		global $db;
		
		$q = "SELECT *, DATE_FORMAT(n_posteddate, '%W %D %M %Y, %l:%i%p') as n_posteddateF,
						 DATE_FORMAT(n_stickyexpiry, '%W %D %M %Y, %l:%i%p') as n_stickyexpiryF,
						 (n_stickyexpiry > NOW()) as n_showsticky
			      FROM " . NEWSARTICLES_TABLE . ", " . CATAGORIES_TABLE . " 
				  WHERE n_id = " . $id . " AND c_id = n_catagory AND c_newscat = 1 AND n_enabled = 1";
		$result = $db->sql_query($q);
		
		if($this->news_article = $db->sql_fetchrow($result)) 
		{
			return true;
		}
		else
			return false;
	}
	
	function saveAdminAction()
	{
		if(isset($_SESSION['valid_admin']) && $_SESSION['valid_admin'] == 3)
		{
			$q = '';
			if(isset($_POST['disable']))
			{
				$q = 'n_enabled = 0';
			}
			else if(isset($_POST['unsticky']))
			{
				$q = 'n_stickyexpiry = NULL';
			}
			else if(isset($_POST['sticky']))
			{
				$q = 'n_stickyexpiry = DATE_ADD(NOW(), INTERVAL 1 WEEK)';
			}
			else if(isset($_POST['comments_show']))
			{
				$q = 'n_commentshide = 0';
			}
			else if(isset($_POST['comments_hide']))
			{
				$q = 'n_commentshide = 1';
			}
			else if(isset($_POST['comments_lock']))
			{
				$q = 'n_commentslocked = 1';
			}
			else if(isset($_POST['comments_unlock']))
			{
				$q = 'n_commentslocked = 0';
			}
			else if(isset($_POST['gallery_hide']))
			{
				$q = 'n_galleryhide = 1';
			}
			else if(isset($_POST['gallery_unhide']))
			{
				$q = 'n_galleryhide = 0';
			}
			
			if($q != '')
			{
				global $db;
			
				$q = 'UPDATE ' . NEWSARTICLES_TABLE . '
				      SET ' . $q . '
					  WHERE n_id = ' . $this->id;
				$db->sql_query($q);  
			}
		}
	
	}
	

}

class newsAdminFunctions
{
	var $buttons = Array();
	var $url;
	var $id;
	
	function newsAdminFunctions($url, $stickied, $comments_hidden, $comments_locked, $gallery_hidden, $id)
	{
		if(! isset($_SESSION['valid_admin']) || $_SESSION['valid_admin'] < 3)
			return;
	
		$this->id = $id;
		global $lang;
		
		$this->buttons['disable'] = array('action' => 'disable', 'text' => $lang['NewsAdminDisable']);
		
		if($stickied)
			$this->buttons['sticky'] = array('action' => 'unsticky', 'text' => $lang['NewsAdminUnsticky']);
		else
			$this->buttons['sticky'] = array('action' => 'sticky', 'text' => $lang['NewsAdminSticky']);
		
		
		if($comments_hidden)
			$this->buttons['comments_hide'] = array('action' => 'comments_show', 'text' => $lang['NewsAdminUnhideComments']);
		else
			$this->buttons['comments_hide'] = array('action' => 'comments_hide', 'text' => $lang['NewsAdminHideComments']);
			
			
		if($comments_locked)
			$this->buttons['comments_lock'] = array('action' => 'comments_unlock', 'text' => $lang['NewsAdminUnlockComments']);
		else
			$this->buttons['comments_lock'] = array('action' => 'comments_lock', 'text' => $lang['NewsAdminLockComments']);
			
			
		if($gallery_hidden)
			$this->buttons['gallery_hide'] = array('action' => 'gallery_show', 'text' => $lang['NewsAdminShowGallery']);
		else
			$this->buttons['gallery_hide'] = array('action' => 'gallery_hide', 'text' => $lang['NewsAdminHideGallery']);
	}
	
	function render()
	{
		if(! isset($_SESSION['valid_admin']) || $_SESSION['valid_admin'] < 3)
			return;
			
		global $lang;
		
		$url = $this->url;
		$buttons = $this->buttons;
		$id = $this->id;
		global $skin_dir;
		
		include $skin_dir . 'tpl/newsAdmin.tpl';
	}
}

function catagory_filter($current_catagory)
{
	global $db;
	
	$q = 'SELECT *, COUNT(*) as article_count
		  FROM ' . CATAGORIES_TABLE . ', ' . NEWSARTICLES_TABLE . '
		  WHERE c_newscat = 1 AND n_catagory = c_id
		  GROUP BY c_id
		  ORDER BY c_title ASC';
		  
	$total_count = 0;
	$catagories = Array();
	
	$result = $db->sql_query($q);
	while($catagory = $db->sql_fetchrow($result)) 
	{
		$catagory['current'] = $catagory['c_id'] == $current_catagory ? 1 : 0;
		$total_count += $catagory['article_count'];
		$catagories[] = $catagory;
	}	
	
	global $lang;
	
	$message = $lang['NewsCatagories'];
	
	if($current_catagory == 0)
	{
		$message .= sprintf($lang['NewsArticleCount'], $lang['NewsAllArticles'], $total_count);
	}
	else
	{
		$message .= '<a href="news.php">' . sprintf($lang['NewsArticleCount'], $lang['NewsAllArticles'], $total_count) . '</a>';
	}
	
	foreach($catagories as $catagory)
	{
		if($catagory['current'] == 1)
			$message .= '|' . sprintf($lang['NewsArticleCount'], $catagory['c_title'], $catagory['article_count']);
		else
			$message .= '|' . '<a href="news.php?catagory=' . $catagory['c_id'] . '">' . 
					sprintf($lang['NewsArticleCount'], $catagory['c_title'], $catagory['article_count']) . '</a>';
			
	}
	
	return $message;
}

?>